package com.ywqf_api.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @author ran
 *
 */
public class BatchNumUtil {
	
	//yyyyMMdd
	private static String day;
	
	private static AtomicInteger num;
	
	
	private static SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
	
	private static final String chars = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
	
	static{
		day = sf.format(new Date());
		num = new AtomicInteger();
	}
	
	private static String generate4RandomCode(){
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < 4; i++) {
			sb.append(chars.charAt(random.nextInt(chars.length()-1)));
		}
		return sb.toString();
	}
	
	private static String getBatchNum(){
		String now =  getNowDayString();
		if (compareDayString(day, now)) {
			day=now;
			num = new AtomicInteger();
		}
		return day + StringUtils.leftPad(String.valueOf(num.getAndIncrement()), 20, "0");
	}
	
	private static String getNowDayString(){
		return sf.format(new Date());
	}
	
	private static boolean compareDayString(String before,String now){
		int beforeNum=Integer.parseInt(before);
		int nowNum=Integer.parseInt(now);
		return nowNum-beforeNum>0?true:false;
	}
	
	public static String generateNumOrderNumber(){
		return generate4RandomCode() + getBatchNum();
	}
	
	public static void main(String[] args) {
	}
}
